import axios from 'axios'
import Bus from './bus'
import util from '@best/libs-cli/libs/util.js';

import ViewUI from 'view-design';

// http request 拦截器
axios.interceptors.response.use(
    response => {
        return response
    },
    //接口错误状态处理，也就是说无响应时的处理
    error => {
        if (error.response.status == 403) {
            Bus.$emit('Illegalcalls');
        } else if (error.response.status == 400) {
            ViewUI.Notice.error({ title: 'Service Error 400', desc: '参数错误，请检查参数信息' });
        } else if (error.response.status == 404) {
            ViewUI.Notice.error({ title: 'Service Error 404', desc: '未找到接口，请检查链接地址' });
        } else if (error.response.status == 500) {
            ViewUI.Notice.error({ title: 'Service Error 500', desc: '服务器出现错误' });
        } else if (error.response.status == 502) {
            ViewUI.Notice.error({ title: 'Service Error 502', desc: '服务器响应出现错误' });
        } else {
            ViewUI.Notice.error({ title: 'Service Error' + error.response.status, desc: '链接异常，请检查错误信息' });
        }
        return Promise.reject(error) // 返回接口返回的错误信息
        // return Promise.reject(error.response.status) // 返回接口返回的错误信息
    })

let AxiosEventCatch = {};

/**
 * 隐藏 loading 方法
 *
 * @param {*} id
 * @memberof Request
 */
const hideLoading = function (id) {
    delete AxiosEventCatch[id];
    if (Object.keys(AxiosEventCatch).length == 0) {
        Bus.$emit('loading-change', false)
    }
}

const Event = function (options) {
    var id = util.Guid();
    if (options.loading !== false) {
        AxiosEventCatch[id] = '';
        Bus.$emit('loading-change', true)
    }
    var baseUrlPath = ''
    if (process.env.NODE_ENV == 'development') {
        try {
            baseUrlPath = window.WIN_COMFIG.service[options.reqType]
        } catch (error) {
            baseUrlPath = window.WIN_COMFIG.service.pro
        }
    } else {
        baseUrlPath = location.protocol
    }

    var _url = baseUrlPath + options.url;
    var _params = JSON.parse(JSON.stringify(options.params));
    _url = _url.replace(/\{\w+\}/g, function (i) {
        var _src = i.replace('{', '').replace('}', '');
        if (_params.hasOwnProperty(_src)) {
            var _value = _params[_src];
            delete _params[_src];
            return _value;
        }
    });

    return axios({
        method: options.type,
        url: _url,
        params: _params,
        data: options.data
    }).then(function (res) {
        if (options.loading !== false) {
            hideLoading(id);
        }
        return res.data;
    }).catch(function (err) {
        if (options.loading !== false) {
            hideLoading(id);
        }
        throw err.response
    });
}

export default {

    /**
     * apis 接口调用参数
     *
     * @param {String} url  链接地址
     * @param {Object} params Url 传参对象
     * @param {Object} data PostData 传参对象
     * @param {String} reqType 开发模式下, 链接地址指向
     * @param {Blob} loading 是否显示loading 开关
     * @return {axios} 接口异步对象
     */
    post(url, params, data, reqType, loading) {
        var _data = {};
        _data.type = 'post';
        _data.data = data || {};
        _data.reqType = reqType;
        _data.params = params;
        _data.url = url;
        _data.loading = loading
        return Event(_data)
    },
    get(url, params, data, reqType, loading) {
        var _data = {};
        _data.type = 'get';
        _data.data = data || {};
        _data.reqType = reqType;
        _data.params = params;
        _data.url = url;
        _data.loading = loading
        return Event(_data)
    },
    put(url, params, data, reqType, loading) {
        var _data = {};
        _data.type = 'put';
        _data.data = data || {};
        _data.reqType = reqType;
        _data.params = params;
        _data.url = url;
        _data.loading = loading
        return Event(_data)
    },
    delete(url, params, data, reqType, loading) {
        var _data = {};
        _data.type = 'delete';
        _data.data = data || {};
        _data.reqType = reqType;
        _data.params = params;
        _data.url = url;
        _data.loading = loading
        return Event(_data)
    }
}


